<template>
  <label
    :class="[prefixCls,
    isButton?'vbestui-radio-border':(isBorder?'vbestui-radio-border':''),
  disabled?'vbestui-radio-disabled':(isAction?'vbestui-radio-border-action':''),
  ]"
  >
    <span
      v-if="!isButton"
      class="vbestui-radio-chec"
      :class="[isAction?'vbestui-radio-chec-action':'',
        isAction?'vbestui-radio-circle':'']"
    ></span>
    <span class="vbestui-radio-input-inner">
      <slot>Radio</slot>
    </span>
    <input
      type="radio"
      :name="name"
      :value="label"
      v-model="model"
      class="vbestui-radio-input-radio"
      @change="change"
      :disabled="disabled"
    />
  </label>
</template>
<script>
const prefixCls="vbestui-radio"
export default {
  name: "BRadio",
  data(){
    return{
      prefixCls:prefixCls,
    }
  },
  props: {
    label: {
      type: [Number, String, Boolean],
      default: "",
    },
    name: {
      type: String,
      default: "",
    },
    value: null,
    border: {
      type: Boolean,
      default: false,
    },
    button: {
      type: Boolean,
      default: false,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  inject: {
    group: {
      default: "",
    },
  },
  computed: {
    //信息监听
    model: {
      get() {
        return this.isGroup ? this.group.value : this.value;
      },
      set() {
        if (!this.disabled) {
          this.isGroup
            ? this.group.$emit("input", this.label)
            : this.$emit("input", this.label);
        }
      },
    },
    isGroup() {
      return !!this.group;
    },
    isAction() {
      //当前选中
      return this.isGroup
        ? this.group.value == this.label
        : this.value == this.label;
    },
    isBorder() {
      return this.group.border || this.border;
    },
    isButton() {
      return this.group.button || this.button;
    },
  },
  methods: {
    change() {
      this.$emit("change");
    },
  },
};
</script>
<style scoped>
.vbestui-radio {
    display: inline-flex;
    align-items: center;
    align-items: center;
    cursor: pointer;
  }
  .vbestui-radio-disabled {
    cursor: no-drop;
  }
  .vbestui-radio-border {
    border: 1px solid var(--border);
    padding: 5px 0px 5px 8px;
    border-radius: 5px;
  }
  .vbestui-radio-border-action {
    border-color: var(--primary);
    color: var(--primary);
    transition: all 0.2s;
  }
  .vbestui-radio-border-action:active {
    transform: translateY(1px);
  }
  .vbestui-radio-chec {
    display: inline-block;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    border: 1px solid #ccc;
    margin-right: 5px;
    position: relative;
  }
  .vbestui-radio-chec-action {
    border-color: var(--primary);
  }
  .vbestui-radio-circle:after {
    display: inline-block;
    content: "";
    width: 10px;
    height: 10px;
    background: var(--primary);
    border-radius: 50%;
    position: absolute;
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px;
    margin: auto;
    animation: fade 0.3s;
  }
  .vbestui-radio-input-radio {
    opacity: 0;
    transform: scale(0);
  }
  @keyframes fade {
    0% {
      opacity: 0;
    }
    100% {
      opacity: 1;
    }
  }
</style>